<template>
  <el-card class="form-container" shadow="never">
    <el-form
      ref="homeAdvertiseFrom"
      :model="homeAdvertise"
      :rules="rules"
      label-width="150px"
      size="small"
    >
      <el-form-item label="轮播名称：" prop="title">
        <el-input v-model="homeAdvertise.title" class="input-width" />
      </el-form-item>
      <el-form-item label="轮播位置：">
        <el-select v-model="homeAdvertise.type">
          <el-option
            v-for="type in typeOptions"
            :key="type.id"
            :label="type.label"
            :value="type.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="图片宽度：" prop="width">
        <el-input v-model="homeAdvertise.width" class="input-width" />
      </el-form-item>

      <el-form-item label="图片高度：" prop="height">
        <el-input v-model="homeAdvertise.height" class="input-width" />
      </el-form-item>


      <el-form-item label="是否启用：">
        <el-radio-group v-model="homeAdvertise.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="轮播图片：">
        <single-upload v-model="homeAdvertise.pic" />
      </el-form-item>
      <!--      <el-form-item label="排序：">-->
      <!--        <el-input v-model="homeAdvertise.sort" class="input-width" />-->
      <!--      </el-form-item>-->
      <el-form-item label="轮播链接：" prop="url">
        <el-input v-model="homeAdvertise.url" class="input-width" />
      </el-form-item>
      <!--      <el-form-item label="广告备注：">-->
      <!--        <el-input-->
      <!--          v-model="homeAdvertise.note"-->
      <!--          class="input-width"-->
      <!--          type="textarea"-->
      <!--          :rows="5"-->
      <!--          placeholder="请输入内容"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="onSubmit('homeAdvertiseFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('homeAdvertiseFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>

  import SingleUpload from '@/components/Upload/singleUpload'
  import { createHomeAdvertise, getHomeAdvertise, updateHomeAdvertise } from '@/api/system/homeAdvertise'
  const defaultTypeOptions = [
    {
      label: 'PC首页轮播',
      value: 0
    },
    {
      label: 'APP首页轮播',
      value: 1
    }
  ];
  const defaultHomeAdvertise = {
    title: null,
    type: 1,
    pic: null,
    status: 0,
    url: null
  };
  export default {
    name: 'HomeAdvertiseDetail',
    components: { SingleUpload },
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
          homeAdvertise: {
              id: null,
              title: null,
              url: null,
              pic: null,
              type: null,
              width: null,
              height: null,
              createdTime: null,
              status: null
          },
        rules: {
          title: [
            { required: true, message: '请输入广告名称', trigger: 'blur' },
            { min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '请输入广告链接', trigger: 'blur' }
          ],
          // startTime: [
          //   { required: true, message: '请选择开始时间', trigger: 'blur' }
          // ],
          // endTime: [
          //   { required: true, message: '请选择到期时间', trigger: 'blur' }
          // ],
          pic: [
            { required: true, message: '请选择广告图片', trigger: 'blur' }
          ]
        },
        typeOptions: Object.assign({}, defaultTypeOptions)
      }
    },
    created() {
      if (this.isEdit) {
        getHomeAdvertise(this.$route.query.id).then(response => {
          this.homeAdvertise = response.data;
        });
      } else {
        this.homeAdvertise = Object.assign({}, defaultHomeAdvertise);
      }
    },
    methods: {
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              if (this.isEdit) {
                  const addParam = {
                      id: this.homeAdvertise.id,
                      title: this.homeAdvertise.title,
                      pic: this.homeAdvertise.pic,
                      url: this.homeAdvertise.url,
                      status: this.homeAdvertise.status,
                      type: this.homeAdvertise.type,
                      width: this.homeAdvertise.width,
                      height: this.homeAdvertise.height
                  }
                updateHomeAdvertise(addParam).then(response => {
                  this.$refs[formName].resetFields();
                  this.$message({
                    message: '修改轮播信息成功',
                    type: 'success',
                    duration: 1000
                  });
                  this.$router.back();
                });
              } else {
                createHomeAdvertise(this.homeAdvertise).then(response => {
                  this.$refs[formName].resetFields();
                  this.homeAdvertise = Object.assign({}, defaultHomeAdvertise);
                    this.$router.back();
                });
              }
            });
          } else {
            this.$message({
              message: '验证失败',
              type: 'error',
              duration: 1000
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.homeAdvertise = Object.assign({}, defaultHomeAdvertise);
      }
    }
  }
</script>
<style scoped>
  .input-width {
    width: 70%;
  }
</style>


